package com.ruoyi.restaurant.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.restaurant.domain.DiningTable;
import com.ruoyi.restaurant.service.IDiningTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/restaurant/table")
public class DiningTableController extends BaseController {
    @Autowired
    private IDiningTableService diningTableService;

    /**
     * 查询餐台列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DiningTable diningTable) {
        startPage();
        List<DiningTable> list = diningTableService.selectDiningTableList(diningTable);
        return getDataTable(list);
    }

    /**
     * 查询所有餐台（不分页）
     */
    @GetMapping("/all")
    public AjaxResult listAll(DiningTable diningTable) {
        List<DiningTable> list = diningTableService.selectDiningTableList(diningTable);
        return success(list);
    }

    /**
     * 获取餐台详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(diningTableService.selectDiningTableById(id));
    }

    /**
     * 新增餐台
     */
    @Log(title = "餐台管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DiningTable diningTable) {
        return toAjax(diningTableService.insertDiningTable(diningTable));
    }

    /**
     * 修改餐台
     */
    @Log(title = "餐台管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DiningTable diningTable) {
        return toAjax(diningTableService.updateDiningTable(diningTable));
    }

    /**
     * 删除餐台
     */
    @Log(title = "餐台管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(diningTableService.deleteDiningTableByIds(ids));
    }

    /**
     * 开台
     */
    @Log(title = "开台", businessType = BusinessType.UPDATE)
    @PostMapping("/open")
    public AjaxResult openTable(@RequestBody DiningTable diningTable) {
        // 设置开台时间为当前时间
        diningTable.setOpenTime(new Date());
        return toAjax(diningTableService.openTable(diningTable));
    }

    /**
     * 换台
     */
    @Log(title = "换台", businessType = BusinessType.UPDATE)
    @PostMapping("/change")
    public AjaxResult changeTable(@RequestBody Map<String, Long> params) {
        Long oldTableId = params.get("oldTableId");
        Long newTableId = params.get("newTableId");
        return toAjax(diningTableService.changeTable(oldTableId, newTableId));
    }

    /**
     * 预定餐台
     */
    @Log(title = "预定餐台", businessType = BusinessType.UPDATE)
    @PostMapping("/reserve")
    public AjaxResult reserveTable(@RequestBody DiningTable diningTable) {
        return toAjax(diningTableService.reserveTable(diningTable));
    }

    /**
     * 结账
     */
    @Log(title = "结账", businessType = BusinessType.UPDATE)
    @PostMapping("/checkout/{id}")
    public AjaxResult checkoutTable(@PathVariable Long id) {
        return toAjax(diningTableService.checkoutTable(id));
    }

    /**
     * 初始化餐台数据
     */
    @Log(title = "初始化餐台", businessType = BusinessType.INSERT)
    @PostMapping("/initialize")
    public AjaxResult initializeTables() {
        return toAjax(diningTableService.initializeTables());
    }
}

